/*
 * @(#) ConfigManager.java  1.0  2003-11-21
 *
 * Copyright 2003 by LongTop Corporation.
 * 2669 XieTu Road, ShangHai, PRC 200030
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * LongTop Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with LongTop.
 */

package com.cnc.bas;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author Du bf
 * @version 1.0  2005-3-26
 */
public class ConfigManager {
    private final static String configFile = "/powerweb.properties";

    private static Log log = LogFactory.getLog(ConfigManager.class);

    /**
     * Singleton implementation function.
     */
    private static ConfigManager theInstance = null;

    //Config file properties

    private Properties theProperties = null;

    /**
     * Default Constructer
     */
    private ConfigManager() {

        //Initialize the nextNumber stuff
        super();
        this.load();
    } /* ConfigManager() */


    /**
     * Return the reference instance of the config manager  Use this in preference
     * to the singleton APIs.
     *
     * @return an instance of ConfigManager
     */
    public static synchronized ConfigManager getInstance() {
        //if(theInstance==null)
        {
            theInstance = new ConfigManager();
        }

        return theInstance;
    } /* getInstance() */

    private void load() {
        InputStream is = ConfigManager.class.getResourceAsStream(configFile);

        if (is == null) { // can not find the name file
            // throw exception
            log.error("Can not find the configuration file \"" + configFile + "\" in the JAVA CLASSPATH");
            return;
        }
        theProperties = new Properties();
        try {
            theProperties.load(is);
        } catch (IOException e) {
            log.error("There is IOException occurred when tried to read from name file " + configFile);
        }
    }

    public Properties getConfig() {
        return theProperties;
    }

    /**
     * 根据Key返回表或配置文件中对应的配置项的值
     *
     * @param key 表或配置文件中配置项的key值
     * @return 培植项的值
     */
    public String getConfigValue(String key) {
        if (theProperties == null) return null;
        return theProperties.getProperty(key);
    }

    /**
     * 根据Key返回表或配置文件中对应的配置项的值,如果表或配置文件中没有指定的项,则返回defaultValue
     *
     * @param key          配置文件中配置项的key值
     * @param defaultValue 配置项的缺省值
     * @return 培植项的值
     */
    public String getConfigValue(String key, String defaultValue) {
        if (theProperties == null) return defaultValue;
        return theProperties.getProperty(key, defaultValue);
    }
}
